namespace pengpeng 
{
    export class PixiFactory extends PPFactory
    {
        constructor()
        {
            super();
        }

        protected _generateTextureAtlasData(textureAtlasData: PixiTextureAtlasData, textureAtlas: PIXI.BaseTexture): PixiTextureAtlasData 
        {
            if (textureAtlasData) {
                textureAtlasData.texture = textureAtlas;
            }
            else {
                textureAtlasData = new PixiTextureAtlasData();
            }
            return textureAtlasData;
        }

        public getTextureDisplay(textureName: string, textureAtlasName: string = null): PIXI.Sprite {
            const textureData = this._getTextureData(textureAtlasName, textureName) as PixiTextureData;
            if (textureData) {
                if (!textureData.texture) {
                    const textureAtlasTexture = (textureData.parent as PixiTextureAtlasData).texture;
                    //const originSize = new PIXI.Rectangle(0, 0, textureData.region.width, textureData.region.height);
                    textureData.texture = new PIXI.Texture(
                        textureAtlasTexture,
                        <PIXI.Rectangle><any>textureData.region,
                        null,
                        null
                    );
                }

                var sp:PIXI.Sprite = new PIXI.Sprite(textureData.texture)
                sp.anchor.x = 0.5;
                sp.anchor.y = 0.5;
                return sp;
            }

            return null;
        }
        public createAsset(assetData:AssetData):PIXI.Sprite
        {
            let assetPath = assetData.path.split("@");
            let textureName:string;
            let subTextureName:string;

            if(assetPath.length == 2)
            {
                textureName = assetPath[0];
                subTextureName = assetPath[1];
            }
            else
            {
                subTextureName = assetData.path;
            }
            return this.getTextureDisplay(subTextureName,textureName);
        }
        public createWorld(name:string):PPWorld
        {
            var phyWorldData:WorldData = this.getWorldData(name);
            if(phyWorldData)
            {
                var box2dWorld = PPBox2D.instance.buildWorld(phyWorldData);
                var pixiBox2dWorld = new PixiBox2DWorld(box2dWorld)
                pixiBox2dWorld.velocityIterations = phyWorldData.velocityIterations;
                pixiBox2dWorld.positionIterations = phyWorldData.positionIterations;

                let i:number;
                let len:number;
                for( i = 0, len = phyWorldData.bodies.length; i < len; i++)
                {
                     let assetData:AssetData = phyWorldData.bodies[i].assetData;
                     if(assetData)
                     {
                         var asset:PIXI.Sprite = this.createAsset(assetData);
                         pixiBox2dWorld.bindBodyView(phyWorldData.bodies[i].name,asset);
                     }
                }
                
                return pixiBox2dWorld;
            }
            return null;
        }

    }  
}